package com.senyint.imsdk.packet;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jivesoftware.smack.packet.PacketExtension;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class AttachmentPacketExtension implements PacketExtension {
	public final static String ELEMENT_NAME = "att";
    public static final String NAMESPACE = "urn:syt:ape";
    
    public final static short TYPE_IMAGE = 0 ;
    public final static short TYPE_VOICE = 1 ;
    public final static short TYPE_VIDEO = 10 ;
    public final static short TYPE_FILE = 20 ;
    public final static short TYPE_LINK = 30 ;
    public final static short TYPE_LOCATION = 40 ;
    public final static short TYPE_GIF = 50 ;
    public final static short TYPE_JSON = 60 ;
    public final static short TYPE_CMD = 999 ;
    
    /**
     * 业务标识,比如图片，语音，视频，通知等
     */
    private String key = null ;
	
    /**
     * 附件类型
     */
	private int type = -1;
	
	/**
	 * 业务编码，数据的唯一标识，如id
	 */
	private String code = null ;
	/**
	 * 附件名称
	 */
	private String name = null ;
	
	/**
	 * 比如图片连接，语音连接<br>
	 */
	private String content = null ;
	
	/**
	 * 文件说明, 如image，有png,jpg,gif，如audio，有amr等
	 */
	private String remark = null ;
	
	private int duration = 0 ;
	
	/**
	 * store file path when uploading file with attachment
	 */
	private String localFile = null ;
	
	@Override
	public String getElementName() {
		return ELEMENT_NAME;
	}

	@Override
	public String getNamespace() {
		return NAMESPACE;
	}

	public int getType() {
		return type;
	}

	public String getRemark() {
		return remark;
	}

	public String getKey() {
		return key;
	}

	public String getCode() {
		return code;
	}

	public String getContent() {
		return content;
	}
	
	public void setContent(String content) {
		this.content = content ;
	}
	
	public String getName() {
		return name;
	}
	
	public void setKey(String key) {
		this.key = key;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}
	
	public String getLocalFile() {
		return localFile;
	}

	public void setLocalFile(String localFile) {
		this.localFile = localFile;
	}

	public AttachmentPacketExtension(){}
	
	public static AttachmentPacketExtension create(int type,String content){
		AttachmentPacketExtension ape = new AttachmentPacketExtension();
		ape.type = type ;
		ape.content = content ;
		return ape ;
	}
	
	/**
	 * 构造消息推展体
	 * @param key		业务类型,可自定义，如报文，事件，通知等
	 * @param name		数据名称,简单描述
	 * @param code		数据标识,消息和内容的在业务中的唯一标识
	 * @param type		数据类型,可自定义，基本指文件类型，如image,audio,json等
	 * @param content	数据,如url,json等
	 * @param remark	说明,对内容的描述，如果是文件，可以是大小;如果是图片，可以是文字描述;如果是语音，可以是语音长度;
	 */
	public static AttachmentPacketExtension create(String key,String name,String code,int type,String content,String remark){
		AttachmentPacketExtension ape = new AttachmentPacketExtension();
		ape.name = name ;
		ape.key = key ;
		ape.code = code ;
		ape.type = type ;
		ape.remark = remark ;
		ape.content = content ;
		return ape ;
	}

	@Override
    public String toXML() {
        StringBuffer sb = new StringBuffer();

        sb.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
        
        if(key != null)
        	sb.append("<key>").append(key).append("</key>");
        else
        	sb.append("<key/>") ;
        
        if(name != null )
        	sb.append("<name>").append(name).append("</name>");
        else
        	sb.append("<name/>") ;
        
        if(code != null)
        	sb.append("<code>").append(code).append("</code>");
        else
        	sb.append("<code/>");
        	
        sb.append("<type>").append(type).append("</type>");
        sb.append("<content>").append(content).append("</content>");
        
        if(remark != null)
        	sb.append("<remark>").append(remark).append("</remark>");
        else
        	sb.append("<remark/>");
        
        sb.append("</"+ELEMENT_NAME+">");

        return sb.toString();
    }
	
	@Override
    public String toString() {
		return toXML();
	}
	
	
    public static AttachmentPacketExtension parse(String xml) {
        AttachmentPacketExtension mmpe = new AttachmentPacketExtension();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
            Element root = document.getDocumentElement();
            NodeList nodeList = root.getChildNodes();
            int size = nodeList.getLength();
            for (int i = 0; i < size; i++) {
                Node node = nodeList.item(i);
                String nodeName = node.getNodeName();
                String value = node.getTextContent();
                if (nodeName.equals("key")) {
                    mmpe.key = value;
                } else if (nodeName.equals("name")) {
                    mmpe.name = value;
                } else if (nodeName.equals("code")) {
                    mmpe.code = value;
                } else if (nodeName.equals("type")) {
                    mmpe.type = Integer.valueOf(value);
                } else if (nodeName.equals("content")) {
                	mmpe.content = value;
                } else if (nodeName.equals("remark")) {
                    mmpe.remark = value;
                } 
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return mmpe;
    }


}
